﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

namespace WebberCross.JavaScriptBridge.WebParts
{
    /// <summary>
    /// This is a base class for asp.net webparts
    /// </summary>
    public class WebPartBase : WebPart
    {
        private string _serviceSource = "";
        
        // Public properties visible to SharePoint UI
        //
        [WebBrowsable(true),
            Category("Configuration"),
            Personalizable(PersonalizationScope.Shared),
            DefaultValue(""),
            WebDisplayName("Service Source"),
            WebDescription("Web Service Source URL")]
        public string ServiceSource
        {
            get { return _serviceSource; }
            set { _serviceSource = value; }
        }               

        /// <summary>
        /// Reads a setting from SharePoint web.config
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        protected string GetConfigSetting(string key)
        {
            try
            {
                return ConfigurationManager.AppSettings[key];
            }
            catch { }
            
            return "Error reading key";           
        }

        protected void RegisterScriptFile(string key, string fileName)
        {
            ClientScriptManager cs = Page.ClientScript;

            if (!cs.IsClientScriptIncludeRegistered(key))
            {
                cs.RegisterClientScriptInclude(this.Page.GetType(), key
                    , fileName);
            }
        }

        protected void RegisterStyleSheet(string fileName)
        {
            // CSS link
            HtmlLink cssLink = new HtmlLink();
            cssLink.Href = fileName;
            cssLink.Attributes["rel"] = "stylesheet";
            cssLink.Attributes["type"] = "text/css";
            this.Page.Header.Controls.Add(cssLink);
        }
    }
}
